package test.n00200;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Problem00240 {
    public static void main(String[] args) {
        println(create(Arrays.asList(1, 2, 3, 4)));
    }

    public static void println(ListNode node) {
        Solution ss = new Solution();
        System.out.println(map(node) + "," + map(ss.swapPairs(node)));
    }

    private static ListNode create(List<Integer> a1) {
        ListNode[] list = new ListNode[a1.size()];
        for (int i = 0; i < a1.size(); i++) {
            list[i] = new ListNode(a1.get(i));
            if (i > 0) {
                list[i - 1].next = list[i];
            }
        }
        return list[0];
    }

    private static List<Integer> map(ListNode head) {
        if (head == null) {
            return Collections.emptyList();
        }
        List<Integer> list = new ArrayList<>();
        while (head != null) {
            list.add(head.val);
            head = head.next;
        }
        return list;
    }

    private static class Solution {

        /**
         * 给你一个链表，两两交换其中相邻的节点，并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题（即，只能进行节点交换）。
         * <p>
         * <p>
         * 提示：
         * <p>
         * 链表中节点的数目在范围 [0, 100] 内
         * 0 <= Node.val <= 100
         * <p>
         * 解题思路:
         * 递归
         *
         * @param head
         * @return
         */
        public ListNode swapPairs(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }

            ListNode newHead = head.next;
            head.next = swapPairs(newHead.next);
            newHead.next = head;
            return newHead;
        }
    }

    private static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
